package tongwentongshu.com.app.presenter;

import android.content.Context;
import android.support.v4.util.ArrayMap;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;

import tongwentongshu.com.app.bean.WechatBean;
import tongwentongshu.com.app.contract.RechargeContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;
import tongwentongshu.com.app.utils.SpUtils;
import tongwentongshu.com.app.wxapi.Constants;

/**
 * Created by XiongBin on 2017/8/1.
 */

public class RechargePresenter implements RechargeContract.Presenter {
    RechargeContract.View rechargeView;

    public RechargePresenter(RechargeContract.View rechargeView) {
        this.rechargeView = rechargeView;
    }

    @Override
    public void payShudou(final Context mContext, String money) {
        Map<String, String> map = new ArrayMap<>();
        map.put("token", Cache.getToken());
        map.put("type", "2");
        map.put("order_money", money);


        UpdateFractory.getBuild()
                .name("UserPayCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<WechatBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(WechatBean item, String msg) {
                        Constants.APP_ID = item.getData().getAppid();
                        IWXAPI msgApi = WXAPIFactory.createWXAPI(mContext, null);
                        msgApi.registerApp(Constants.APP_ID);
                        PayReq mPayReq = new PayReq();
                        mPayReq.appId = item.getData().getAppid();
                        mPayReq.partnerId = item.getData().getPartnerid();// 商户号
                        mPayReq.prepayId = item.getData().getPrepayid(); // 预支付交易会话ID
                        mPayReq.packageValue = item.getData().getPackageX();// 扩展字段
                        mPayReq.nonceStr = item.getData().getNoncestr(); // 随机字符串
                        mPayReq.timeStamp = item.getData().getTimestamp(); // 时间戳
                        mPayReq.sign = item.getData().getSign();// 签名
                        SpUtils.setString(mContext, "pay", Cache.PAY_DOPSIT);
                        msgApi.sendReq(mPayReq);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }
}
